import teamCservlets.*;
import java.util.ArrayList;

public class TestDatabaseInterface {

	public static void main(String args[]) {

		PersonBean person;

		ApplicationBean app;

		DefectBean defect;

		DatabaseInterfacePerson interfacePerson = new DatabaseInterfacePerson();
		DatabaseInterfaceApplication interfaceApplication = new DatabaseInterfaceApplication();
		DatabaseInterfaceDefect interfaceDefect = new DatabaseInterfaceDefect();

		// Test ability to get person by an e-mail address.

		/*System.out.println("** TESTING METHOD TO GET PERSON BY E-MAIL ADDRESS **");

		person = interfacePerson.getPerson("rmukai68@gmail.com");

		System.out.println("** TESTING METHOD TO GET PERSON BY PERSON ID **");
		interfacePerson = new DatabaseInterfacePerson();
		person = interfacePerson.getPersonByID(1);


		System.out.println("** TESTING METHOD TO GET APPLICATION BY APPLICATION ID **");

		app = interfaceApplication.getApplication(1);

		System.out.println(app.getApplicationID());
		System.out.println(app.getApplicationName());

		// Load defect 1 from the database (defect ID = 1).

		System.out.println("** TESTING METHOD TO GET DEFECT BY DEFECT ID **");

		defect = interfaceDefect.getDefect(1);

		System.out.println("DEFECT ID = " + defect.getDefectID());
		System.out.println("APPLICATINO ID = " + defect.getApplicationID());
		System.out.println("SUBMITTER ID = " + defect.getSubmitterID());

		// Get the associated defect submitter.

		System.out.println("** Get the associated defect submitter **");
		interfacePerson = new DatabaseInterfacePerson();
		person = interfacePerson.getPersonByID(defect.getSubmitterID());

		// Get the associated defect assignee.

		System.out.println("** Get the associated defect assignee **");
		interfacePerson = new DatabaseInterfacePerson();
		person = interfacePerson.getPersonByID(defect.getAssigneeID());
		System.out.println("The person is: " + person);

		// Get the associated application

		System.out.println("** Get the associated application **");
		interfaceApplication = new DatabaseInterfaceApplication();
		app = interfaceApplication.getApplication(defect.getApplicationID());

		// Write a new defect.  This should fail with a bad application ID.

		interfaceDefect = new DatabaseInterfaceDefect();
		boolean testFlag = interfaceDefect.writeNewDefect(8, 2, 1,
				"HIGH", "E-R Diagrams Corrupted", "E-R Diagrams have half their arrows deleted on load",
				"2014-08-14");
		System.out.println("Result of trying to add an invalid defect: " + testFlag);

		// Write a new defect.  This should work with a good application ID.

		interfaceDefect = new DatabaseInterfaceDefect();
		testFlag = interfaceDefect.writeNewDefect(4, 2, -1,
				"HIGH", "E-R Diagrams Corrupted", "E-R Diagrams have half their arrows deleted on load",
				"2014-08-14");
		System.out.println("Result of trying to add a valid defect: " + testFlag);

		// Write a new defect.  This should work with a good application ID.

		interfaceDefect = new DatabaseInterfaceDefect();
		testFlag = interfaceDefect.writeNewDefect(3, 2, 1,
				"HIGH", "Slides corrupted", "Slides display in inverse video",
				"2014-08-14");
		System.out.println("Result of trying to add a valid defect: " + testFlag);

		// Edit the defect with ID 3 involving E-R diagram corruption on save.


		interfaceDefect = new DatabaseInterfaceDefect();
		DefectBean defectER = interfaceDefect.getDefect(3);

		// Put in a resolution.  Change status to closed.  Write out to database.

		defectER.setStringResolveDate("2014-08-20");
		defectER.setResolutionNote("Modified FileSave.java.  Regression testing shows defect fixed.");
		defectER.setStatus("CLOSED");


		// Write the defect back to the database.
		interfaceDefect = new DatabaseInterfaceDefect();
		testFlag = interfaceDefect.writeModifiedDefect(defectER.getDefectID(),defectER.getAssigneeID(),
			defectER.getStatus(),defectER.getPriority(),defectER.getSummary(),defectER.getDetailedDescription(),
			defectER.getResolutionNote(),defectER.getStringResolveDate());

		System.out.println("Result of trying to modified a defect: " + testFlag);

		// For defect ID 1, we'll assign it in the first modification step.  We'll close it entirely in the
		// second modification test.

		interfaceDefect = new DatabaseInterfaceDefect();
		defectER = interfaceDefect.getDefect(1);

		// First attempt will FAIL because there is no possible assignee with ID 5.
		defectER.setAssigneeID(5);
		defectER.setStatus("ASSIGNED");

		// Write the defect back to the database.  This should FAIL.
		interfaceDefect = new DatabaseInterfaceDefect();
		testFlag = interfaceDefect.writeModifiedDefect(defectER.getDefectID(),defectER.getAssigneeID(),
			defectER.getStatus(),defectER.getPriority(),defectER.getSummary(),defectER.getDetailedDescription(),
			defectER.getResolutionNote(),defectER.getStringResolveDate());

		System.out.println("Result of trying to make an invalid modification: " + testFlag);

		// Second attempt will work with a good assignee ID.
		interfaceDefect = new DatabaseInterfaceDefect();
		defectER = interfaceDefect.getDefect(1);

		// Second attempt should work.
		defectER.setAssigneeID(3);
		defectER.setStatus("ASSIGNED");

		// Write the defect back to the database.  This should FAIL.
		interfaceDefect = new DatabaseInterfaceDefect();
		testFlag = interfaceDefect.writeModifiedDefect(defectER.getDefectID(),defectER.getAssigneeID(),
			defectER.getStatus(),defectER.getPriority(),defectER.getSummary(),defectER.getDetailedDescription(),
			defectER.getResolutionNote(),defectER.getStringResolveDate());

		System.out.println("Result of trying to set a valid assignee: " + testFlag);

		// Now close it out
		interfaceDefect = new DatabaseInterfaceDefect();
		defectER = interfaceDefect.getDefect(1);

		// Put in a resolution.  Change status to closed.  Write out to database.

		defectER.setStringResolveDate("2014-08-25");
		defectER.setResolutionNote("Modified FileLoad.java.  Now handles files well beyond 1 MB.");
		defectER.setStatus("CLOSED");


		// Write the defect back to the database.
		interfaceDefect = new DatabaseInterfaceDefect();
		testFlag = interfaceDefect.writeModifiedDefect(defectER.getDefectID(),defectER.getAssigneeID(),
			defectER.getStatus(),defectER.getPriority(),defectER.getSummary(),defectER.getDetailedDescription(),
			defectER.getResolutionNote(),defectER.getStringResolveDate());

		System.out.println("Result of trying to closes a defect: " + testFlag); */

		// Test the new methods to get lists of people, applications, and defects.

		ArrayList<ApplicationBean> appList;
		ArrayList<PersonBean> personList;
		ArrayList<DefectBean> defectList;

		interfaceDefect = new DatabaseInterfaceDefect();

		defectList = interfaceDefect.getOpenDefectList();

		interfacePerson = new DatabaseInterfacePerson();

		personList = interfacePerson.getPersonList();

		interfaceApplication = new DatabaseInterfaceApplication();

		appList = interfaceApplication.getApplicationList();


		for(PersonBean currentPerson : personList) {
			System.out.println();
			System.out.println("PersonID = " + currentPerson.getId());
		}

		for(ApplicationBean currentApp : appList) {
			System.out.println();
			System.out.println("ApplicationID = " + currentApp.getApplicationID());
		}

		for(DefectBean currentDefect : defectList) {
			System.out.println();
			System.out.println("DefectID = " + currentDefect.getDefectID());
			System.out.println("Status = " + currentDefect.getStatus());
			System.out.println("Summary = " + currentDefect.getSummary());
		}
	} // End main method

} // End class TestDefectBean